//
//  AXFClassifyGoodsCell.m
//  爱鲜蜂
//
//  Created by HH on 16/11/4.
//  Copyright © 2016年 胡浩. All rights reserved.
//

#import "AXFClassifyGoodsCell.h"
#import "AXFClassifyLabel.h"
#import "AXFDetailsModel.h"

#import "AXFModelManager.h"
@interface AXFClassifyGoodsCell ()

@property (nonatomic,weak)UIImageView *pictureView;//图片

@property (nonatomic,weak)UILabel *nameLabel;//名称

@property (nonatomic,weak)UILabel *capacityLabel;//容量

@property (nonatomic,weak)UILabel *discountPriceLabel;//折扣价

@property (nonatomic,weak)UILabel *originPriceLabel;//原价

@end

@implementation AXFClassifyGoodsCell

- (void)setupUI
{
    //图片view
    UIImageView *pictureView = [[UIImageView alloc] init];
    
    _pictureView = pictureView;
    
    pictureView.backgroundColor = [UIColor cz_randomColor];
    
    [self.contentView addSubview:pictureView];
    
    //名称label
    UILabel *nameLabel = [UILabel cz_labelWithText:@"爱鲜蜂·冬枣" fontSize:12 color:[UIColor blackColor]];
    
    _nameLabel = nameLabel;
    
    nameLabel.numberOfLines = 1;

    [self.contentView addSubview:nameLabel];
    
    UIImageView *eliteView = [[UIImageView alloc] init];
    
    eliteView.image = [UIImage imageNamed:@"jingxuan"];
    
    [self.contentView addSubview:eliteView];
    
    //容量label
    UILabel *capacityLabel = [UILabel cz_labelWithText:@"400-450g/盒" fontSize:13 color:[UIColor cz_colorWithHex:0x898989]];
    
    _capacityLabel = capacityLabel;
    
    [self.contentView addSubview:capacityLabel];
    
    //折扣价label
    UILabel *discountPriceLabel = [UILabel cz_labelWithText:@"¥9.9" fontSize:13 color:[UIColor cz_colorWithHex:0xff4400]];
    
    _discountPriceLabel = discountPriceLabel;
    
    [self.contentView addSubview:discountPriceLabel];
    
    //原价label
    UILabel *originPriceLabel = [UILabel cz_labelWithText:nil fontSize:12 color:[UIColor cz_colorWithHex:0x898989]];
    
    _originPriceLabel = originPriceLabel;
    
    [self.contentView addSubview:originPriceLabel];
    
    //v2_increase
    
    UIButton *increaseBtn = [[UIButton alloc] init];
    
    [increaseBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [increaseBtn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
    
    [self.contentView addSubview:increaseBtn];
	
	[increaseBtn addTarget:self action:@selector(increaseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //MARK:布局
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.height.mas_equalTo(120);
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.right.offset(-6);
        make.top.equalTo(pictureView.mas_bottom);
    }];
    
    [eliteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(nameLabel.mas_bottom).offset(4);
        make.size.mas_equalTo(CGSizeMake(30, 16));
    }];
    
    [capacityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(eliteView.mas_bottom).offset(4);
    }];
    
    [discountPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.top.equalTo(capacityLabel.mas_bottom).offset(4);
    }];
    
    [originPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(discountPriceLabel);
        make.left.equalTo(discountPriceLabel.mas_right).offset(4);
    }];
    
    [increaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(eliteView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.right.offset(-5);
    }];
}

- (void)increaseBtnAction:(UIButton *)sender {
	//获取主窗口    需要计算  点击的按钮在View上的坐标  所以  直接在主窗口上获取比较方便
	UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
	//定义一个属性   cell可以访问的到   然后获取点击按钮的位置
	CGPoint picturePoint = [self convertPoint:self.pictureView.center toView:keyWindow];
	
	if ([_delegate respondsToSelector:@selector(addGoodsWithCell:model:goodsPicLoc:)]) {
		[_delegate addGoodsWithCell:self model:_model goodsPicLoc:picturePoint];
	}
}


- (void)setModel:(AXFDetailsModel *)model

{
    _model = model;
    
    self.nameLabel.text = model.name;
    
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_square"]];
    
    self.capacityLabel.text = model.specifics;
    
    //MARK:富文本
    //一定要用可变的
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",model.market_price]];
    
    [text setAttributes:@{NSStrikethroughStyleAttributeName :@(1),NSStrikethroughColorAttributeName:[UIColor cz_colorWithHex:0x898989]} range:NSMakeRange(0, text.length)];
    
    self.originPriceLabel.attributedText = text;
    
    self.discountPriceLabel.text = [NSString stringWithFormat:@"¥%@",model.price];
    
}

- (void)clickBtn:(UIButton *)sender
{
     [[NSNotificationCenter defaultCenter] postNotificationName:@"addNumber" object:nil];
    
    [AXFModelManager shareModelManager];
    
    _model.shopCount += 1;
    NSDictionary *userInfo = @{@"model":_model};
    [[NSNotificationCenter defaultCenter]   postNotificationName:@"shoppingCart" object:self userInfo:userInfo];
}

@end
